package com.cn.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("开启服务监听，端口号：8888");

        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("接收到了一个客户端连接");
            new Thread(new ServerHandler(socket)).start();
        }
    }

    static class ServerHandler implements Runnable {
        private Socket socket;

        public ServerHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream is = socket.getInputStream();
                OutputStream os = socket.getOutputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

                while (true) {
                    String line = br.readLine();
                    System.out.println("收到了来自客户端的数据：" + line);

                    bw.write("hello bio client,i am bioserver \n");
                    bw.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
